package v1

import (
	"bc/service"
	"bc/util"
	"bc/util/library/zstack"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"strconv"
)

type zone struct {

}
func (t *zone) Create(c *gin.Context){
	d := make(map[string]interface{})

	name ,ok := c.GetPostForm("name")
	if !ok{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"请填写名称",
		})
		return
	}
	d["params"] = map[string]interface{}{
		"name":name,
		"description" : c.DefaultPostForm("desc",""),
		"state":c.DefaultPostForm("state","Enabled"),
		"type":c.DefaultPostForm("type","zstack"),
	}
	result , err :=zstack.Zone.Create(d)
	if err != nil {
		c.JSON(500,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":result,
	})
}
func (t *zone) CreateCallBack(c *gin.Context){

	b,e := ioutil.ReadAll(c.Request.Body)
	if e != nil{
		panic(e)
	}
	defer c.Request.Body.Close()

	m := make(map[string]interface{})

	err := json.Unmarshal(b,&m)
	if err != nil{
		panic(err)
	}

	if v,ok := m["error"];ok{
		con := v.(map[string]interface{})["details"].(string)
		LogAdd(c,con,"区域管理")
		return
	}
	nm := m["inventory"].(map[string]interface{})
	nm["uid"] = c.GetInt("uid")
	bool,i := service.Zone.Create(nm)
	if bool {
		LogAdd(c,"添加区域" + strconv.Itoa(i),"区域管理")
	}

}
func (t *zone) Delete(c *gin.Context){

	uuid := c.Param("uuid")
	if uuid == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"请填写uuid",
		})
		return
	}

	r,e := zstack.Zone.Delete(uuid,"Permissive")
	if e!= nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":r,
	})

}
func (t *zone) DeleteCallBack(c *gin.Context){
	uuid := c.Query("uuid")
	m := map[string]interface{}{
		"zone_uuid":uuid,
	}
	b := service.Zone.Delete(m)
	LogAdd(c,"删除区域" + uuid,"区域管理")
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":b,
	})
}
func (t *zone)Update(c *gin.Context){

	uuid := c.Param("uuid")
	if uuid == "" {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	data := make(map[string]map[string]interface{})
	data["updateZone"]= make(map[string]interface{})
	if v,ok := c.GetPostForm("name");ok{
		data["updateZone"]["name"] = v
	}else{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"请输入区域名称",
		})
		return
	}
	if v,ok := c.GetPostForm("desc");ok{
		data["updateZone"]["description"] = v
	}else{
		data["updateZone"]["description"] = ""
	}
	byte,err := json.Marshal(data)
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	str,err := zstack.Zone.Update(uuid,string(byte))
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"编辑成功",
		"data":str,
	})
}
func (t *zone)UpdateCallBack(c *gin.Context){

	uuid := c.Query("uuid")
	if uuid == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	byte,err := ioutil.ReadAll(c.Request.Body)
	defer c.Request.Body.Close()
	fmt.Println(string(byte))
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	data := make(map[string]map[string]interface{})
	err = json.Unmarshal(byte,&data)
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	inv := make(map[string]interface{})
	inv = data["inventory"]
	fmt.Println(inv)
	bool := service.Zone.Update(map[string]interface{}{"zone_uuid":uuid},inv)
	if bool {
		c.JSON(200,gin.H{
			"code":200,
			"msg":"更新成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
	})

}
func (t *zone)UpdateState(c *gin.Context){

	uuid := c.Param("uuid")
	if uuid == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"uuid参数有误",
		})
		return
	}
	data := make(map[string]map[string]interface{})
	data["changeZoneState"] = make(map[string]interface{})
	if v,ok := c.GetPostForm("state");ok{

		state := []string{"enable","disable"}
		isExist := false
		for _,vs := range state{
			if vs == v{
				isExist = true
				break
			}
		}
		if isExist == false{
			c.JSON(200,gin.H{
				"code":500,
				"msg":"error",
				"data":"启用状态值只能为[enable,disable]",
			})
			return
		}

		data["changeZoneState"]["stateEvent"] = v
	}else{
		data["changeZoneState"]["stateEvent"] = "enable"
	}
	byte,err := json.Marshal(data)
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"数据有误，请重试",
		})
		return
	}
	str,err := zstack.Zone.ChangeZoneState(uuid,string(byte))
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":str,
	})
}
func (t *zone)UpdateStateCallBack(c *gin.Context){

	byte,err := ioutil.ReadAll(c.Request.Body)
	defer c.Request.Body.Close()
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	data := make(map[string]interface{})
	data["error"] = make(map[string]interface{})

	err = json.Unmarshal(byte,&data)
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}

	if len(data["error"].(map[string]interface{})) > 0{
		errdata := make(map[string]interface{})
		errdata = data["error"].(map[string]interface{})
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":errdata["details"],
		})
		fmt.Println(errdata["details"])
		return
	}

	inv := make(map[string]interface{})
	inv = data["inventory"].(map[string]interface{})
	fmt.Println(inv["state"])
	bool := service.Zone.UpdateState(map[string]interface{}{"zone_uuid":inv["uuid"]},map[string]interface{}{"state":inv["state"]})
	if bool {
		c.JSON(200,gin.H{
			"code":200,
			"msg":"更新成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
	})

}
func (t *zone)Find(c *gin.Context){

	m := map[string]interface{}{}
	page,_ := strconv.Atoi(c.DefaultQuery("page","1"))
	pageSize := c.DefaultQuery("pageSize",strconv.FormatInt(util.Config.App.PageSize,10))
	NpageSize,_ := strconv.Atoi(pageSize)
	data,total,err := service.Zone.Find(m,page,NpageSize)
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":data,
		"total":total,
	})
}
func (t *zone)First(c *gin.Context){
	uuid := c.Param("uuid")
	if uuid == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	err,data := service.Zone.First(uuid)
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":data,
	})
}
var Zone = &zone{}
